﻿//////////////////////////////////////////////////////////////////
//
// QuantityResult.cs
//
// Copyright (c) 2012 Dan Pike. All rights reserved
//
// Refer to license.txt for any license restrictions.
// 
using Entities;
using Entities.Cities;
using Logger4Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sally
{
   ///////////////////////////////////////////////////////////////////////////
   /// <summary>
   /// 
   /// </summary>
   public class QuantityResult
   {
      ///////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// 
      /// </summary>
      public QuantityResult(World world)
      {
         world_ = world;
         population_ = 0;
         selectedCity_ = false;
         selectedConvoy_ = false;
         protectFromTrade_ = createCountMapping();
         protectFromMarket_ = createCountMapping();
         loadFromHub_ = createCountMapping();
         factoryCount_ = createCountMapping();
         convoyPercent_ = new Dictionary<SallyConvoy, int>();
      }

      ///////////////////////////////////////////////////////////////////////////
      //
      public Dictionary<Material, int> protectFromTrade
      {
         get { return protectFromTrade_; }
         set { protectFromTrade_ = value; }
      }

      ///////////////////////////////////////////////////////////////////////////
      //
      public Dictionary<Material, int> protectFromMarket
      {
         get { return protectFromMarket_; }
         set { protectFromMarket_ = value; }
      }

      ///////////////////////////////////////////////////////////////////////////
      //
      public Dictionary<Material, int> totalLoadFromHub
      {
         get { return loadFromHub_; }
         set { loadFromHub_ = value; }
      }

      ///////////////////////////////////////////////////////////////////////////
      //
      public int convoyPercentCount
      {
         get { return convoyPercent_.Count; }
      }

      ///////////////////////////////////////////////////////////////////////////
      //
      public int getLoadFromHub(Material material, SallyConvoy convoy)
      {
         try
         {
            return (loadFromHub_[material] * convoyPercent_[convoy]) / 100;
         }
         catch (System.Exception ex)
         {
            logger_.exception(ex);
         }
         return 0;
      }

      ///////////////////////////////////////////////////////////////////////////
      //
      public void setConvoyPercent(SallyConvoy convoy, int percent)
      {
         try
         {
            if (!convoyPercent_.ContainsKey(convoy))
            {
               convoyPercent_.Add(convoy, percent);
            }
            else
            {
               convoyPercent_[convoy] = percent;
            }
         }
         catch (System.Exception ex)
         {
            logger_.exception(ex);
         }
      }

      ///////////////////////////////////////////////////////////////////////////
      //
      public Dictionary<Material, int> factoryCount
      {
         get { return factoryCount_; }
         set { factoryCount_ = value; }
      }

      //////////////////////////////////////////////////////////////////////////
      //
      public Dictionary<Material, int> createCountMapping()
      {
         Dictionary<Material, int> result = null;
         try
         {
            result = new Dictionary<Material, int>();
            world_.almanac.materials.ForEach(material => result.Add(material, 0));
         }
         catch (System.Exception ex)
         {
            logger_.exception(ex);
         }
         return result;
      }

      ///////////////////////////////////////////////////////////////////////////
      //
      public int population
      {
         get { return population_; }
         set { population_ = value; }
      }

      ///////////////////////////////////////////////////////////////////////////
      //
      public int capacity
      {
         get { return capacity_; }
         set { capacity_ = value; }
      }

      ///////////////////////////////////////////////////////////////////////////
      //
      public bool selectedCity
      {
         get { return selectedCity_; }
         set { selectedCity_ = value; }
      }

      ///////////////////////////////////////////////////////////////////////////
      //
      public bool selectedConvoy
      {
         get { return selectedConvoy_; }
         set { selectedConvoy_ = value; }
      }

      ///////////////////////////////////////////////////////////////////////////
      //
      public List<SallyConvoy> convoys
      {
         get { return convoyPercent_.Keys.ToList(); }
      }

      ///////////////////////////////////////////////////////////////////////////
      //
      private Logger logger_ = new Logger(typeof(QuantityResult));
      private World world_;
      private int population_;
      private int capacity_;
      private bool selectedCity_;
      private bool selectedConvoy_;
      private Dictionary<Material, int> protectFromTrade_;
      private Dictionary<Material, int> protectFromMarket_;
      private Dictionary<Material, int> loadFromHub_;
      private Dictionary<Material, int> factoryCount_;
      private Dictionary<SallyConvoy, int> convoyPercent_;
   }
}
